package com.example.forum.utils;

import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

/**
 * 功能描述:
 *
 * @author Lenovo
 * @date 2024/12/12
 */

@Slf4j
public class MD5Util {
    //加密
    public static String encrypt(String password, String salt) {
        if (!StringUtils.hasLength(password) || !StringUtils.hasLength(salt)) {
            throw new ApplicationException(ResultCode.FAILED_PARAMS_VALIDATE.toString());
        }
        String securityPassword = DigestUtils.md5DigestAsHex((salt + password).getBytes(StandardCharsets.UTF_8));
        return securityPassword;
    }

    public static Boolean verify(@NonNull String salt, @NonNull String password, @NonNull String userPassword) {
        String securityPassword = encrypt(password, salt);
        return securityPassword.equalsIgnoreCase(userPassword);
    }
}
